package com.sy.struts;

import java.util.List;

import org.springside.core.dao.Page;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import com.sy.model.STest;
import com.sy.service.ISTestService;
import com.sy.struts.base.PageActionSupport;
import com.sy.util.DateJsonValueProcessor;

public class HelloWorld extends PageActionSupport {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 8412371860202120773L;
	private ISTestService iSTestService;
	private STest st;
	private String id;
	public String execute(){
		return SUCCESS;
	}
	public String say(){
		return SUCCESS;
	}
	public String list(){      
		JsonConfig cfg = new JsonConfig();   		
		cfg.registerJsonValueProcessor(java.util.Date.class,new DateJsonValueProcessor("yyyy-MM-dd")) ;        
        //每页显示条数   
        int number = Integer.parseInt((getRows() == null || getRows() == "0") ? "10":getRows());   
		int intPage = (getPage() == null || getPage() == "0") ? 0:(Integer.parseInt(getPage())-1)*number;
        
		List<STest> list = iSTestService.getAllStest(intPage,number);//每页的数据，放入list  
		setResult(new JSONObject());
//        Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map   
//        jsonMap.put("total", iSTestService.count());//total键 存放总记录数，必须的   
//        jsonMap.put("rows", list);//rows键 存放每页记录 list           
		getResult().put("rows",JSONArray.fromObject(list,cfg));   
		getResult().put("total", iSTestService.count());//当分页建立时设置记录的总数量  
		return "jsonlist";
	}
	public String addStest(){
		try{
			setResult(new JSONObject());
			iSTestService.saveSTest(st);
			getResult().put("resultStr","保存成功");
		}catch(Exception e){
			setResult(new JSONObject());
			getResult().put("resultStr","保存失败");
		}
		return "jsonlist";
	}
	public String getStest(){
		STest st=iSTestService.getSTest(id);
		setResult(new JSONObject());
		getResult().put("st", st);
		return "jsonlist";
	}
	public String updateStest(){
		try{
			setResult(new JSONObject());
			iSTestService.updateSTest(st);;
			getResult().put("resultStr","更新成功");
		}catch(Exception e){
			setResult(new JSONObject());
			getResult().put("resultStr","更新失败");
		}
		return "jsonlist";
	}
	public String delStest(){
		try{
			setResult(new JSONObject());
			iSTestService.deleteSTest(id);
			getResult().put("resultStr","删除成功");
		}catch(Exception e){
			setResult(new JSONObject());
			getResult().put("resultStr","删除失败");
		}
		return "jsonlist";
	}
	public String selectStestByVal(){
		if(id!=null&&!id.equals("")){
		JsonConfig cfg = new JsonConfig();   		
		cfg.registerJsonValueProcessor(java.util.Date.class,new DateJsonValueProcessor("yyyy-MM-dd")) ;        
        //每页显示条数   
        int number = Integer.parseInt((getRows() == null || getRows() == "0") ? "10":getRows());   
		int intPage = (getPage() == null || getPage() == "0") ? 0:(Integer.parseInt(getPage())-1)*number;
        
		setResult(new JSONObject());
		Page page=	iSTestService.getAllStestByVal(intPage, number, new Object[]{id});
		getResult().put("rows",JSONArray.fromObject(page.getItems(),cfg));   
		getResult().put("total", page.getTotalCount());//当分页建立时设置记录的总数量  
		return "jsonlist";}else{
			return list();
		}
	}
	public ISTestService getiSTestService() {
		return iSTestService;
	}
	public void setiSTestService(ISTestService iSTestService) {
		this.iSTestService = iSTestService;
	}
	public STest getSt() {
		return st;
	}
	public void setSt(STest st) {
		this.st = st;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
}
